﻿using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Flock/Behavior/Cohesion")]
public class CohesionBehavior : FlockBehavior
{
    Vector2 desiredVelocity;

    public override Vector2 Force(FlockItem item, List<Transform> neighbors)
    {
        int count = neighbors.Count;
        if (count == 0) return Vector2.zero;

        Vector2 force = Vector2.zero;
        Vector2 massCenter = Vector2.zero;

        for (int i = 0; i < count; i++)
        {
            massCenter += (Vector2)neighbors[i].position;
        }

        massCenter /= (float) count;

        force = massCenter - (Vector2)item.transform.position;

        return force;
    }
}
